<template>
	<view class="address">

		<!-- 无数据 -->
		<view class="address-nodata" v-if="addressList !== null && addressList.length === 0">
			<image class="address-nodata__bg" mode="widthFix" :src="$imgUrl('/address/bg-nodata.png')"></image>
			<view class="address-nodata__text">暂无收货地址</view>
		</view>

		<!-- 地址列表 -->
		<view class="address-list" v-if="addressList !== null && addressList.length > 0">
			<uni-swipe-action>
				<uni-swipe-action-item v-for="(item, index) in addressList" :key="index">
					<view class="address-list__box" @click="goBack(item)">
						<view class="address-list__box__top">{{ item.name }} {{ item.phone }}</view>
						<view class="address-list__box__bot">{{ item.area }}{{ item.address }}</view>
						<image @click.stop="editAddress(item)" class="address-list__box__icon" mode="widthFix"
							:src="$imgUrl('/address/icon-edit.png')"></image>
					</view>
					<template v-slot:right>
						<view class="address-list__del" @click="delAddress(item.id)">删除</view>
					</template>
				</uni-swipe-action-item>
			</uni-swipe-action>
		</view>

		<!-- 固底按钮 -->
		<view class="address-bot">
			<view class="address-bot__wechat btn">
				<image class="address-nodata__icon" mode="widthFix" :src="$imgUrl('/address/icon-wechat.png')"></image>
				<view>从微信导入</view>
			</view>
			<view class="address-bot__toadd btn" @click="addNewAddress">
				<image class="address-nodata__icon" mode="widthFix" :src="$imgUrl('/address/icon-add.png')"></image>
				<view>添加新地址</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				addressList: null
			}
		},
		onShow() {
			this.getAddressList()
		},
		onUnload() {
			uni.$off()
		},
		methods: {
			goBack(item) {
				uni.$emit('selectUser', item)
				uni.navigateBack()
			},
			async getAddressList() {
				const data = await this.$apis.address.getAddress()
				console.info(data)
				this.addressList = data.data
			},
			addNewAddress() {
				uni.navigateTo({
					url: '/pages/address/addAddress'
				})
			},
			delAddress(id) {
				uni.showLoading()
				let that = this
				this.$apis.address.delAddress({
						id
					})
					.then(res => {
						uni.hideLoading()
						that.getAddressList()
					})
			},
			editAddress(item) {
				const params = JSON.stringify(item)
				uni.navigateTo({
					url: '/pages/address/editAddress?params=' + params
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.address {
		width: calc(100vw - 40rpx);
		min-height: 100vh;
		padding: 20rpx;
		padding-bottom: 120rpx;
		background: #F6F6F6;

		.address-nodata {
			position: fixed;
			top: 0;
			left: 0;
			z-index: 1;
			width: 100%;
			height: 100%;
			background: #ffffff;
			display: flex;
			flex-direction: column;
			align-items: center;
			justify-content: center;

			.address-nodata__bg {
				width: 381rpx;
				height: 264rpx;
				margin-bottom: 48rpx;
			}

			.address-nodata__text {
				font-size: 30rpx;
				color: #333333;
			}
		}

		.address-bot {
			width: calc(100vw - 40rpx);
			padding: 10rpx 20rpx;
			position: fixed;
			left: 0;
			bottom: 0;
			z-index: 2;
			display: flex;
			align-items: center;
			justify-content: space-between;

			.address-nodata__icon {
				width: 48rpx;
				height: 42rpx;
			}

			.btn {
				width: 343rpx;
				height: 80rpx;
				border-radius: 40rpx;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 30rpx;
				font-weight: bold;
			}

			.address-bot__wechat {
				color: #666666;
				background: #FFFFFF;
				border: 1px solid #666666;
			}

			.address-bot__toadd {
				border: 1px solid #2FCC71;
				background: #2FCC71;
				color: #FFFFFF;
			}
		}

		.address-list {

			.address-list__box {
				height: 102rpx;
				background: #FFFFFF;
				border-radius: 16rpx;
				padding: 32rpx 24rpx;
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				position: relative;
				margin-bottom: 20rpx;
			}

			.address-list__box__icon {
				width: 44rpx;
				height: 44rpx;
				position: absolute;
				top: 50%;
				right: 24rpx;
				transform: translateY(-50%);
			}

			.address-list__box__top {
				font-size: 32rpx;
				font-weight: bold;
				color: #333333;
			}

			.address-list__box__bot {
				font-size: 28rpx;
				color: #666666;
			}

			.address-list__del {
				width: 120rpx;
				height: 166rpx;
				background: #FB4041;
				display: flex;
				align-items: center;
				justify-content: center;
				font-size: 28rpx;
				color: #FFFFFF;
			}
		}
	}
</style>